<?php
/**
 * @copyright: ©2024 永州 微聚商网络科技有限公司
 * @link: https://www.wEijuyunke.com
 */
namespace app\mobile\controller;

use app\BaseController;
use think\App;
use think\facade\Request;
use think\facade\Db;
use think\facade\Log;
use think\Model;
use app\admin\model\FigureUser;
use app\admin\model\FigureScreenAvatar;
use app\admin\model\FigureVoiceTwin;
use app\admin\model\FigureAvatar;
use app\admin\model\FigureScreenLog;
use app\common\service\ApiService;

/**
 * 大屏操作
 *
 * @author Administrator
 *
 */
class Screen extends BaseController
{

    /**
     * 形象列表
     * http://aigc.weijuyunke.cn/mobile/screen/avatarList
     *
     * @param
     *            page 页码
     * @param
     *            psize 每页条数
     */
    public function avatarList()
    {
        try {

            $pindex = max(1, intval(input('page')));
            $psize = empty(input('psize')) ? 10 : intval(input('psize'));

            $where = " is_display = 1 ";

            $total = FigureScreenAvatar::where($where)->count();
            if ($total) {
                $list = FigureScreenAvatar::where($where)->order('sort', 'desc')
                    ->limit(($pindex - 1) * $psize, $psize)
                    ->select();

                foreach ($list as $key => $value) {

                    if (empty($value['avatar_img'])) {
                        $list[$key]['avatar_img'] = $value['avatar_video'] . "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto";
                    }
                }
            } else {
                $list = array();
            }

            $totalPage = ceil($total / $psize);

            return result(0, 'success', array(
                'list' => $list,
                'total' => $total,
                'pindex' => $pindex,
                'psize' => $psize,
                'totalPage' => $totalPage
            ));
        } catch (\Exception $e) {
            return result(- 1, '异常' . $e->getLine() . "--->" . $e->getMessage());
            exit();
        }
    }

    /**
     * 音频试听
     * http://aigc.weijuyunke.cn/mobile/screen/sendTts
     *
     * @param
     *            avatar_id 形象id
     * @param
     *            msg 文本
     * @param
     *            speechRate 语速 1是正常速度。这个值越大，语速越快
     */
    public function sendTts()
    {
        try {

            $avatar_id = input('avatar_id');

            $avatar = FigureScreenAvatar::where('id', $avatar_id)->find();
            if (empty($avatar)) {
                return result(- 1, '形象不存在');
            }

            $msg = input('msg');
            if (empty($msg)) {
                return result(- 1, 'msg必传');
            }

            $speechRate = empty(input('speechRate')) ? 1 : input('speechRate');

            $voice = FigureVoiceTwin::where('id', $avatar['voice_id'])->find();

            $apiService = new ApiService();
            if ($avatar['voice_mode'] == 1) { // 标准模式
                $data = array(
                    'msg' => $msg,
                    'speechRate' => $speechRate,
                    'user_voice_id' => $voice['_id'],
                    'country' => 'zh',
                    'region' => 'CN'
                );

                $result = $apiService->sendTts($data);
            } elseif ($avatar['voice_mode'] == 2) { // 高保真

                $result = $apiService->ttsVoice([
                    'id' => $voice['_id'],
                    'text' => $msg,
                    'speedRatio' => $speechRate
                ]);
            }

            if (isset($result['code']) && $result['code'] == 1) {
                return result(0, 'success', $result['data']);
            } else {
                return result(- 1, $result['msg']);
            }
        } catch (\Exception $e) {
            return result(- 1, '异常' . $e->getLine() . "--->" . $e->getMessage());
            exit();
        }
    }

    /**
     * 视频生成
     * http://aigc.weijuyunke.cn/mobile/screen/generate
     *
     * @param
     *            avatar_id 形象id
     * @param
     *            audio_src 音频链接
     */
    public function generate()
    {
        try {

            $avatar_id = input('avatar_id');

            $screenAvatar = FigureScreenAvatar::where('id', $avatar_id)->find();
            if (empty($screenAvatar)) {
                return result(- 1, '形象不存在');
            }

            $audio_src = input('audio_src');
            if (empty($audio_src)) {
                return result(- 1, '音频必传');
            }

            $avatar = FigureAvatar::where('id', $screenAvatar['avatar_id'])->find();

            $params = array(

                'screen_id' => $screenAvatar['id'],
                'avatar_id' => $screenAvatar['avatar_id'],
                'voice_id' => $screenAvatar['voice_id'],
                'anchor_type' => 1,
                'anchor_id' => $avatar['anchor_id'],
                'audio_src' => $audio_src,
                'web_bg_width' => 1080,
                'web_bg_height' => 1920,
                'web_people_width' => 1080,
                'web_people_height' => 1920,
                'web_people_x' => 0,
                'web_people_y' => 0,
                'is_skip_rs' => 1,
                'is_allow_reverse' => 1
            );

            $screenLog = new FigureScreenLog();
            $screenLog->save($params);

            $title = date('ymd') . rand(999, 9999);
            $params['title'] = $title;

            $apiService = new ApiService();
            $result = $apiService->generate($params);

            if (isset($result['code']) && $result['code'] == 1) {

                $screenLog->save([
                    '_id' => $result['data']['id'],
                    'current_status' => $result['data']['current_status']
                ]);

                return result(0, '生成成功');
            } else {

                $screenLog->save([
                    '_id' => isset($result['data']['id']) ? $result['data']['id'] : '',
                    'current_status' => 'fail'
                ]);

                return result(- 1, $result['msg']);
            }
        } catch (\Exception $e) {
            return result(- 1, '异常' . $e->getLine() . "--->" . $e->getMessage());
            exit();
        }
    }

    /**
     * 作品列表
     * http://aigc.weijuyunke.cn/mobile/screen/logList
     *
     * @param
     *            page 页码
     * @param
     *            psize 每页条数
     */
    public function logList()
    {
        try {

            $pindex = max(1, intval(input('page')));
            $psize = empty(input('psize')) ? 10 : intval(input('psize'));

            $where = " current_status != 'failed'  ";

            $total = FigureScreenLog::where($where)->count();
            if ($total) {
                $list = FigureScreenLog::where($where)->order('create_time', 'desc')
                    ->limit(($pindex - 1) * $psize, $psize)
                    ->select();
            } else {
                $list = array();
            }

            $totalPage = ceil($total / $psize);

            return result(0, 'success', array(
                'list' => $list,
                'total' => $total,
                'pindex' => $pindex,
                'psize' => $psize,
                'totalPage' => $totalPage
            ));
        } catch (\Exception $e) {
            return result(- 1, '异常' . $e->getLine() . "--->" . $e->getMessage());
            exit();
        }
    }
}